/**
 * The MIT License
 *
 * Copyright (c) 2010 Benjamin Klum
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.helgoboss.scala_swing_additions

import scala.swing._
import javax.swing.SwingUtilities

class ProgressView(
        host: Option[Window] = None, 
        dialogTitle: String = "Operation"
    ) {
    

    private lazy val progressDialog = new Dialog(host getOrElse null) {
        centerOnScreen
        title = dialogTitle
        modal = true
        contents = new BoxPanel(Orientation.Vertical) {
            contents += progressBar
            contents += descriptionLabel
        }
    }
    
    private lazy val descriptionLabel = new Label
    
    private lazy val progressBar = new ProgressBar {
        indeterminate = true
    }
    
    /**
     * This executes the given operation and shows a progress bar in the meanwhile. This method blocks.
     */
    def showForOperation(description: String, indeterminate: Boolean = true)(operation: => Unit) {
        updateDescription(description)
        def showProgressDialog {
            progressDialog.pack
            progressDialog.visible = true
        }
        def executeOperationAndReset {
            try {
                operation
            } finally {
                Swing.onEDT {
                    reset
                }
            }
        }
        if (SwingUtilities.isEventDispatchThread) {
            /* This method is called from the Event Dispatch Thread (triggered by a frontend mouse click etc.), so we have to start 
             * a new thread that runs the operation. Following thread makes the operation run in background and makes the progress 
             * dialog disappear afterwards.
             */
            new Thread {
                override def run {
                    executeOperationAndReset
                }
            }.start
        
            /* The following call shows the progress bar and blocks */
            showProgressDialog
        } else {
            /* This method is called from a normal thread (triggered by a backend operation), so we can do it the straight-forward
             * way.
             */
            Swing.onEDT {
                showProgressDialog
            }
            executeOperationAndReset
        }
    }
    
    private def reset {
        progressDialog.visible = false
        progressBar.value = 0
        descriptionLabel.text = ""
    }
    
    def updateDescription(description: String) {
        Swing.onEDT {
            descriptionLabel.text = description
        }
    }
    
    def updateProgress(progress: Int) {
        Swing.onEDT {
            progressBar.indeterminate = false
            progressBar.value = progress
        }
    }
    
    def dispose {
        Swing.onEDT {
            progressDialog.dispose
        }
    }
}